#include <concepts>
#include <coroutine>
#include <exception>
#include <iostream>

struct ReturnObject
{
    struct promise_type
    {
        ReturnObject get_return_object()
        {
            return {};
        }
        std::suspend_never initial_suspend()
        {
            return {};
        }
        std::suspend_never final_suspend() noexcept
        {
            return {};
        }
        void unhandled_exception()
        {
        }
    };
};

struct Awaiter
{
    std::coroutine_handle<> *hp_;
    constexpr bool await_ready() const noexcept
    {
        return false;
    }
    constexpr void await_resume() const noexcept
    {
    }
    void await_suspend(std::coroutine_handle<> h)
    {
        if (hp_)
        {
            *hp_ = h;
            hp_ = nullptr;
        }
    }
};

// task任务，需要传入一个corohandle并与之绑定
ReturnObject counter(std::coroutine_handle<> *cntnu_out)
{
    Awaiter a{cntnu_out};
    for (size_t i = 0;; ++i)
    {
        co_await a;
        std::cout << "Counter: " << i << std::endl;
    }
}

void test1()
{
    std::coroutine_handle<> h;
    counter(&h);  //绑定到handle
    for (int i = 0; i < 3; ++i)
    {
        std::cout << "In test1()\n";
        h();
    }
    h.destroy();
}

int main(int argc, char **argv)
{
    test1();
    return 0;
}
